<?php

/**
 * This is the model class for table "itm_motul".
 *
 * The followings are the available columns in table 'itm_motul':
 * @property integer $id
 * @property string $articul
 * @property string $alias
 * @property string $title
 * @property string $sae
 * @property string $standart
 * @property string $description
 * @property string $volume
 * @property integer $in_pack
 * @property double $price_in
 * @property integer $price_out
 * @property integer $fixed_price
 * @property string $page_title
 * @property string $page_description
 * @property string $page_keywords
 */
class Motul extends CActiveRecord implements IECartPosition
{
	
	public 
	$images = array(
		'photo' => array(
			'field' => 'photo',
			'filename' => '%alias%',
			'alt' => 'Горюче-смазочный материал %title%',
			'title' => '%title%', 
			'addWatermark' => true,
			'bg' => '#FFFFFF',
			'file_type' => 'jpg',
			'sizes' => array(
				'small'=>array(113, 113),
				'normal' => array(200,200),
				'big'=>array(400, 400),
			),
		),
	);
	
	/*
	 * Методы интерфейса IECartPosition
	 */
	function getId(){
		return __CLASS__.'_'.$this->getPrimaryKey();
	}
	
	/*
	 * Методы интерфейса IECartPosition
	 */
	function getPrice(){
		return $this->price_out;
	}
	
	/*
	 * Методы интерфейса IECartPosition
	 */
	function getTitle(){
		return $this->title;
	}
	
	function getUrl(){
		$params = array(
			'oils/view', 
			'usage'=>L::item('gsmUsage',$this->type), 
			'part'=>L::item('gsmType',$this->part_id),
			'slug0'=>'motornoe_maslo_motul',
			'slug1'=>$this->alias,
			'id'=>$this->id,
		);

		return Yii::app()->createUrl($params[0], array_slice($params, 1));
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Motul the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'itm_motul';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('alias, title, volume, in_pack, type, price_in, price_out', 'required'),
			array('in_pack, type', 'numerical', 'integerOnly'=>true),
			array('price_in, price_out, part_id', 'numerical'),
			array('articul, sae, volume', 'length', 'max'=>80),
			array('alias, title, photo', 'length', 'max'=>255),
			array('fixed_price', 'boolean'),
			array('standart, description, page_title, page_description, page_keywords', 'safe'),
			
			array('photo', 'file', 'types'=>'jpg, gif, png, jpeg', 'maxSize' => 10490000, 'allowEmpty'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, articul, alias, title, sae, standart, type, part_id, description, volume, in_pack, price_in, price_out, fixed_price, page_title, page_description, page_keywords', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 	'ID',
			'articul' => 'Артикул',
			'alias' => 'Псевдоним',
			'title' => 'Название',
			'type' => 'Применяемость',
			'part_id' => 'Тип',
			'photo' => 'Фото',
			'sae' => 'SAE',
			'standart' => 'Стандарты и одобрения',
			'description' => 'Описание',
			'volume' => 'Ед.Бнк/Бочка',
			'in_pack' => 'Шт. в упаковке',
			'price_in' => 'Оптовая цена (НДС)',
			'price_out' => 'Розничная цена (НДС)',
			'fixed_price' => 'Фиксированная цена',
			'page_title' => 'Тайтл страницы',
			'page_description' => 'Описание страницы',
			'page_keywords' => 'Ключевые слова',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('articul',$this->articul,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('part_id',$this->part_id);
		$criteria->compare('type',$this->type);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('sae',$this->sae,true);
		$criteria->compare('standart',$this->standart,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('volume',$this->volume,true);
		$criteria->compare('in_pack',$this->in_pack);
		$criteria->compare('price_in',$this->price_in);
		$criteria->compare('price_out',$this->price_out);
		$criteria->compare('fixed_price',$this->fixed_price);
		$criteria->compare('page_title',$this->page_title,true);
		$criteria->compare('page_description',$this->page_description,true);
		$criteria->compare('page_keywords',$this->page_keywords,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}